﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using HCMServices.DTO;
using System.Collections.Generic;
using System.Linq;
using System.Data.Linq.Mapping;
using System.Data.Linq;

namespace HCMServices.DAO
{
    /// <summary>
    /// This class process DUONG Table
    /// </summary>
    public class StreetDAO
    {
        private MyDataContext db;

        /// <summary>
        /// Constructor of class
        /// </summary>
        /// <param name="connectString">String indentify how to connect database</param>
        public StreetDAO(string connectString)
        {
            db = new MyDataContext(connectString);
        }

        /// <summary>
        /// This function search DUONG row base on ID Column 
        /// </summary>
        /// <param name="id">ID to find</param>
        /// <returns>DUONG row if id was found or null</returns>
        public StreetDTO Search(int id)
        {
            StreetDTO duongDTO = new StreetDTO();

            IList<DUONG> query = (from DUONG duong in db.Street
                                      where duong.MaDuong == id
                                      select duong).ToList();

            foreach (DUONG d in query)
            {
                duongDTO.Id = d.MaDuong;
                duongDTO.Name = d.TenDuong;
                return duongDTO;
            }

            return null;
        }

        /// <summary>
        /// This function gets all DUONG rows in database
        /// </summary>
        /// <returns>List of DUONG rows</returns>
        public List<StreetDTO> GetAllDuongDTO()
        {
            List<StreetDTO> lstDuongDTO = new List<StreetDTO>();

            IList<DUONG> query = (from DUONG d in db.Street
                                  select d).ToList();

            foreach (DUONG d in query)
            {
                StreetDTO temp = new StreetDTO();
                temp.Id = d.MaDuong;
                temp.Name = d.TenDuong;

                lstDuongDTO.Add(temp);
            }

            return lstDuongDTO;
        }

        /// <summary>
        /// This function retrieve id of DUONG base on TenDuong
        /// </summary>
        /// <param name="name">String of TenDuong want to find</param>
        /// <returns>MaDuong of DUONG was found or -1</returns>
        public int GetIdByName(string name)
        {
            int maDuong = -1;

            IList<DUONG> query = (from DUONG d in db.Street
                                  where d.TenDuong == name
                                  select d).ToList();

            foreach (DUONG d in query)
            {
                maDuong = d.MaDuong;
                return maDuong;             
            }

            return maDuong;                 
        }
    }
}